package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.crforecastsales.bo.ForecastOrderBO;
import com.epoch.app.crforecastsales.bo.ForecastOrderLineBO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yuanwei
 */
public class ForecastOrderBOToSDOConvertor {

    private static final Log log = Log.getLogger(ForecastOrderBOToSDOConvertor.class);

    public static ForecastOrderSDO convert(ForecastOrderBO forecastOrderBO) {
        ForecastOrderSDO forecastOrderSDO = new ForecastOrderSDO();
        forecastOrderSDO.setId(forecastOrderBO.getId());
        forecastOrderSDO.setOuterOrderId(forecastOrderBO.getOuterOrderId());
        forecastOrderSDO.setOrderBizId(forecastOrderBO.getOrderBizId());
        forecastOrderSDO.setSaleChannelCode(forecastOrderBO.getSalesChannelCode());
        forecastOrderSDO.setSaleChannelName(forecastOrderBO.getSalesChannelName());
        forecastOrderSDO.setOrganizationCode(forecastOrderBO.getSalesOrganizationId());
        forecastOrderSDO.setOrganizationName(forecastOrderBO.getSalesOrganizationName());
        forecastOrderSDO.setDepartmentCode(forecastOrderBO.getSalesDepartmentId());
        forecastOrderSDO.setDepartmentName(forecastOrderBO.getSalesDepartmentName());
        forecastOrderSDO.setCustomerCode(forecastOrderBO.getSoldToPartyCode());
        forecastOrderSDO.setCustomerName(forecastOrderBO.getSoldToPartyName());
        forecastOrderSDO.setWareHouseCode(forecastOrderBO.getShippingWarehouseId());
        forecastOrderSDO.setWareHouseName(forecastOrderBO.getShippingWarehouseName());
        forecastOrderSDO.setShippingWarehouseCode(forecastOrderBO.getShippingWarehouseCode());
        forecastOrderSDO.setShippingWarehouseId(forecastOrderBO.getShippingWarehouseId());
        forecastOrderSDO.setShippingWarehouseName(forecastOrderBO.getShippingWarehouseName());
        forecastOrderSDO.setDeliveryFactoryCode(forecastOrderBO.getDeliveryFactoryCode());
        forecastOrderSDO.setStockCode(forecastOrderBO.getStockCode());
        forecastOrderSDO.setChannelCode(forecastOrderBO.getChannelCode());
        forecastOrderSDO.setChannelName(forecastOrderBO.getChannelName());
        forecastOrderSDO.setSalesmanCode(forecastOrderBO.getSalesmanCode());
        forecastOrderSDO.setSalesmanName(forecastOrderBO.getSalesmanName());
        forecastOrderSDO.setReceiveDetailAddress(forecastOrderBO.getShipToPartyName());
        forecastOrderSDO.setReceiveAddressCode(forecastOrderBO.getShipToPartyCode());
        forecastOrderSDO.setContact(forecastOrderBO.getContact());
        forecastOrderSDO.setContactPhone(forecastOrderBO.getContactPhone());
        forecastOrderSDO.setVaildBeginTime(Objects.isNull(forecastOrderBO.getVaildBeginTime()) ? new Date() : forecastOrderBO.getVaildBeginTime());

        forecastOrderSDO.setOrderTag(forecastOrderBO.getOrderTag());
        forecastOrderSDO.setOrderType(forecastOrderBO.getOrderType());
        forecastOrderSDO.setProductGroupCode(forecastOrderBO.getProductGroupCode());
        forecastOrderSDO.setProductGroupName(forecastOrderBO.getProductGroupName());
        forecastOrderSDO.setTransportModeCode(forecastOrderBO.getTransportModeCode());
        forecastOrderSDO.setTransportModeName(forecastOrderBO.getTransportModeName());
        forecastOrderSDO.setCreateReason(forecastOrderBO.getCreateReasonCode());
        forecastOrderSDO.setCreateReasonName(forecastOrderBO.getCreateReasonName());
        forecastOrderSDO.setRemark(forecastOrderBO.getRemark());
        forecastOrderSDO.setCreditBalance(forecastOrderBO.getCreditBalance());
        forecastOrderSDO.setCustomerReferenceNo(forecastOrderBO.getCustomerPurchaseOrderNo());
        forecastOrderSDO.setCustomerPurchaseOrderNo(forecastOrderBO.getCustomerPurchaseOrderNo());
        forecastOrderSDO.setSalesOrganizationId(forecastOrderBO.getSalesOrganizationId());
        forecastOrderSDO.setSalesOrganizationName(forecastOrderBO.getSalesOrganizationName());
        forecastOrderSDO.setSalesOrganizationCode(forecastOrderBO.getSalesOrganizationCode());

        forecastOrderSDO.setSalesChannelId(forecastOrderBO.getSalesChannelId());
        forecastOrderSDO.setSalesChannelName(forecastOrderBO.getSalesChannelName());
        forecastOrderSDO.setSalesChannelCode(forecastOrderBO.getSalesChannelCode());

        forecastOrderSDO.setSalesDepartmentId(forecastOrderBO.getSalesDepartmentId());
        forecastOrderSDO.setSalesDepartmentName(forecastOrderBO.getSalesDepartmentName());
        forecastOrderSDO.setSalesDepartmentCode(forecastOrderBO.getSalesDepartmentCode());
        if (StringUtils.isNotBlank(forecastOrderBO.getCustomerPurchaseDate())) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = simpleDateFormat.parse(forecastOrderBO.getCustomerPurchaseDate());
                forecastOrderSDO.setCustomerPurchaseDate(parse);
            } catch (Exception e) {
                log.error("ForecastOrderBOToSDOConvertor#convert error", e);
            }
        }
        if (StringUtils.isNotBlank(forecastOrderBO.getCustomerExpectTDate())) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = simpleDateFormat.parse(forecastOrderBO.getCustomerExpectTDate());
                forecastOrderSDO.setCustomerExpectTDate(parse);
            } catch (Exception e) {
                log.error("ForecastOrderBOToSDOConvertor#convert error", e);
            }
        }
        //操作人员
        forecastOrderSDO.setCreatorId(forecastOrderBO.getCreatorId());
        forecastOrderSDO.setCreatorCode(forecastOrderBO.getCreatorCode());
        forecastOrderSDO.setCreatorName(forecastOrderBO.getCreatorName());
        forecastOrderSDO.setUpdater(forecastOrderBO.getModifierId());
        forecastOrderSDO.setCreator(forecastOrderBO.getCreatorId());
        forecastOrderSDO.setModifierId(forecastOrderBO.getModifierId());
        forecastOrderSDO.setModifierCode(forecastOrderBO.getModifierCode());
        forecastOrderSDO.setModifierName(forecastOrderBO.getModifierName());

        forecastOrderSDO.setFeatures(forecastOrderBO.getFeatures());
        forecastOrderSDO.setBisUnitCode(forecastOrderBO.getBisUnitCode());
        forecastOrderSDO.setSalesmanCode(forecastOrderBO.getSalesmanCode());
        forecastOrderSDO.setSalesmanName(forecastOrderBO.getSalesmanName());
        forecastOrderSDO.setCustomerPrincipalId(forecastOrderBO.getCustomerPrincipalId());
        forecastOrderSDO.setCustomerPrincipalName(forecastOrderBO.getCustomerPrincipalName());
        forecastOrderSDO.setFromSys(forecastOrderBO.getFromSys());
        forecastOrderSDO.setBusinessType(forecastOrderBO.getBusinessType());
        forecastOrderSDO.setBusinessTypeName(forecastOrderBO.getBusinessTypeName());
        forecastOrderSDO.setBusinessUnitCode(forecastOrderBO.getBusinessUnitCode());
        forecastOrderSDO.setBusinessUnitName(forecastOrderBO.getBusinessUnitName());
        forecastOrderSDO.setOrderTypeCode(forecastOrderBO.getOrderTypeCode());
        forecastOrderSDO.setOrderTypeName(forecastOrderBO.getOrderTypeName());
        forecastOrderSDO.setSourceOrderNo(forecastOrderBO.getSourceOrderNo());
        forecastOrderSDO.setBizFeatures(forecastOrderBO.getBizFeatures());
        forecastOrderSDO.setReqNo(forecastOrderBO.getReqNo());
        forecastOrderSDO.setStatus(forecastOrderBO.getStatus());

        // 一盘货新增字段
        forecastOrderSDO.setOrderCategoryCode(forecastOrderBO.getOrderCategoryCode());
        forecastOrderSDO.setOrderChannelCode(forecastOrderBO.getOrderChannelCode());
        forecastOrderSDO.setReceiveWarehouseCode(forecastOrderBO.getReceiveWarehouseCode());
        forecastOrderSDO.setReceiveWarehouseName(forecastOrderBO.getReceiveWarehouseName());
        forecastOrderSDO.setReceiveFactoryCode(forecastOrderBO.getReceiveFactoryCode());
        forecastOrderSDO.setReceiveStockCode(forecastOrderBO.getReceiveStockCode());
        forecastOrderSDO.setFulfillmentTypeCode(forecastOrderBO.getFulfillmentTypeCode());
        forecastOrderSDO.setFulfillmentPriority(forecastOrderBO.getFulfillmentPriority());
        forecastOrderSDO.setAutoFulfillment(forecastOrderBO.getAutoFulfillment());
        forecastOrderSDO.setKaSystem(forecastOrderBO.getKaSystem());
        forecastOrderSDO.setKaArea(forecastOrderBO.getKaArea());
        forecastOrderSDO.setCustomerGrade(forecastOrderBO.getCustomerGrade());
        forecastOrderSDO.setCustomerGroup(forecastOrderBO.getCustomerGroup());
        forecastOrderSDO.setCustomerGroupName(forecastOrderBO.getCustomerGroupName());
        forecastOrderSDO.setVaildBeginTime(forecastOrderBO.getVaildBeginTime());
        forecastOrderSDO.setValidEndTime(forecastOrderBO.getValidEndTime());
        if(StringUtils.isNotBlank(forecastOrderBO.getSourceOrderDate())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date sourceOrderDate = sdf.parse(forecastOrderBO.getSourceOrderDate());
                forecastOrderSDO.setSourceOrderDate(sourceOrderDate);
            } catch (Exception e) {
                log.error("ForecastOrderBOToSDOConvertor#convert error", e);
            }
        }
        forecastOrderSDO.setForecastOrderLineSDOList(convertOrderLine(forecastOrderBO.getForecastOrderLineBOList()));
        return forecastOrderSDO;
    }

    private static List<ForecastOrderLineSDO> convertOrderLine(List<ForecastOrderLineBO> orderLines) {
        if (CollectionUtils.isEmpty(orderLines)) {
            return Lists.newArrayList();
        }
        List<ForecastOrderLineSDO> responses = Lists.newArrayList();
        for (ForecastOrderLineBO orderLine : orderLines) {
            ForecastOrderLineSDO response = lineBOToSDO(orderLine);
            responses.add(response);
        }
        return responses;
    }

    public static ForecastOrderLineSDO lineBOToSDO(ForecastOrderLineBO lineBO) {
        ForecastOrderLineSDO lineSDO = ForecastOrderLineSDO.builder()
                .id(lineBO.getId())
                .refuseReasonCode(lineBO.getRefuseReasonCode())
                .refuseReason(lineBO.getRefuseReason())
                .closeFlag(lineBO.getCloseFlag())
                .serialNumber(lineBO.getLineNum())
                .shouldPayFee(lineBO.getShouldPayFee())
                .serialNumber(lineBO.getLineNum())
                .detailRemark(lineBO.getDetailRemark())
                .outerLineId(lineBO.getOuterLineId())
                .sourceLineNum(lineBO.getSourceLineNum())
                .goodsCode(lineBO.getProductCode())
                .scItemTitle(lineBO.getProductName())
                .itemCode(lineBO.getItemCode())
                .itemBarCode(lineBO.getItemBarCode())
                .saleUnitCode(lineBO.getSaleUnitCode())
                .saleUnit(lineBO.getSaleUnitName())
                .inventoryUnitCode(lineBO.getInventoryUnitCode())
                .inventoryUnit(lineBO.getInventoryUnitName())
                .shippingWarehouseCode(lineBO.getShippingWarehouseCode())
                .shippingWarehouseId(lineBO.getShippingWarehouseId())
                .shippingWarehouseName(lineBO.getShippingWarehouseName())
                .deliveryFactoryCode(lineBO.getDeliveryFactoryCode())
                .stockCode(lineBO.getStockCode())
                .unitConvert(lineBO.getUnitConvert())
                .unitPrice(lineBO.getUnitPrice())
                .activityPrice(lineBO.getActivityPrice())
                .status(lineBO.getStatus())
                .modifierId(lineBO.getModifierId())
                .modofierCode(lineBO.getModifierCode())
                .modifierName(lineBO.getModifierName())
                .categoryCode(lineBO.getCategoryCode())
                .categoryDesc(lineBO.getCategoryDesc())
                .categoryChildCode(lineBO.getCategoryChildCode())
                .categoryChildDesc(lineBO.getCategoryChildDesc())
                .firstCategoryCode(lineBO.getFirstCategoryCode())
                .firstCategoryDesc(lineBO.getFirstCategoryDesc())
                .secondCategoryCode(lineBO.getSecondCategoryCode())
                .secondCategoryDesc(lineBO.getSecondCategoryDesc())
                .mainOrderBizId(lineBO.getMainOrderBizId())
                .mainOrderId(lineBO.getMainOrderId())
                .creatorId(lineBO.getCreatorId())
                .creatorCode(lineBO.getCreatorCode())
                .creatorName(lineBO.getCreatorName())
                .updater(lineBO.getUpdater())
                .status(lineBO.getStatus())
                .analysisMessage(lineBO.getAnalysisMessage())
                .analysisReason(lineBO.getAnalysisReason())
                .analysisTime(lineBO.getAnalysisTime())
                .activityCode(lineBO.getActivityCode())
                .activityNumber(lineBO.getActivityNumber())
                .activityName(lineBO.getActivityName())
                .scItemId(lineBO.getProductId())
                .quantity(lineBO.getQuantity())
                .totalFee(lineBO.getTotalFee())
                .activityPrice(lineBO.getActivityPrice())
                .totalActivityFee(lineBO.getTotalActivityFee())  // 折扣后总金额
                .goodsProductationTime(lineBO.getGoodsProductationTime())
                .confirmQuantity(lineBO.getConfirmQuantity())
                .innerProductCode(lineBO.getInnerProductCode())
                .innerProductName(lineBO.getInnerProductName())
                .bizFeatures(lineBO.getBizFeatures())
                .sourceUnit(lineBO.getSourceUnit())
                .sourceQuantity(lineBO.getSourceQuantity())
                .sourcePrice(lineBO.getSourcePrice())
                .actualTotalReplyAmount(lineBO.getActualTotalReplyAmount())
                .build();

        Map<String, String> features = lineBO.getFeatures();
        if (Objects.isNull(features)) {
            features = new HashMap<>();
        }
        features.put("giftQuantity", lineBO.getGiftQuantity());
        features.put("taxRate", lineBO.getTaxRate());
        lineSDO.setFeatures(features);

        return lineSDO;
    }
}
